import {getContainerSources, getStorageSources, WORKSTATE, workSwitch} from "./public/CreepPublicAction.js";

/**
 * 建造者工作逻辑
 * @param {Creep} creep
 * @param sourcesIndex
 */
export const builderRun = (creep, sourcesIndex) => {
    //工作逻辑转换
    workSwitch(creep, RESOURCE_ENERGY)
    if (creep.memory[WORKSTATE]) {
        workCore(creep)
    } else {
        getSources(creep, sourcesIndex)
    }

};

const workCore = (creep) => {
    const target = creep.pos.findClosestByRange(FIND_CONSTRUCTION_SITES);
    if (target) {
        if (creep.build(target) === ERR_NOT_IN_RANGE) {
            creep.moveTo(target, {visualizePathStyle: {stroke: '#ffffff'}});
        }
    } else {
        //找到其它房间的建筑工地
        for (const roomsKey in Game.rooms) {
            const targets = Game.rooms[roomsKey].find(FIND_CONSTRUCTION_SITES)
            if (targets.length > 0) {
                if (creep.build(targets[0]) === ERR_NOT_IN_RANGE) {
                    creep.moveTo(targets[0]);
                }
            }
        }
    }
}

const getSources = (creep, sourcesIndex) => {
    // getStorageSources(creep, 0)
    if (!getStorageSources(creep, sourcesIndex, "#0800ff")) {
        getContainerSources(creep, sourcesIndex, "#0800ff", 1000, true)
    }

}